/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id$
 */

/**
 * \file
 *         Source file for function addComponent in component capabilitiesView.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "autoPnP/adv/capabilitiesView/include/addComponentFunction.h"

#include "autoPnP/adv/capabilitiesView/include/addComponentFunctionWrapper.h"
#include "autoPnP/adv/capabilitiesView/include/capabilitiesViewComponent.h"
#include "autoPnP/adv/capabilitiesView/include/capabilitiesViewComponentWrapper.h"
#include "autoPnP/adv/capabilitiesView/include/capabilitiesViewManifest.h"

#include "xme/core/logUtils.h"

#include "xme/hal/include/mem.h"

// PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_C_INCLUDES) ENABLED START
#include <stdbool.h>
// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_C_DEFINITIONS) ENABLED START
// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/

// PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_C_VARIABLES) ENABLED START
// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_C_PROTOTYPES) ENABLED START
int autoPnP_adv_capabilitiesView_capabilitiesViewComponent_compareComponents(autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t* component1, autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t* component2);
// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
autoPnP_adv_capabilitiesView_addComponentFunction_init
(
    autoPnP_adv_capabilitiesView_capabilitiesViewComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_INITIALIZE_C) ENABLED START
	XME_UNUSED_PARAMETER(componentConfig);

	return XME_STATUS_SUCCESS;
    // PROTECTED REGION END
}

void
autoPnP_adv_capabilitiesView_addComponentFunction_step
(
    autoPnP_adv_capabilitiesView_capabilitiesViewComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    AutoPnP_topic_add_component_t portAddComponentData; // Required port.
    
    (void)xme_hal_mem_set(&portAddComponentData, 0u, sizeof(AutoPnP_topic_add_component_t));
    
    status[0] = autoPnP_adv_capabilitiesView_capabilitiesViewComponentWrapper_readPortAddComponent(&portAddComponentData);
    
    {
        // PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_STEP_C) ENABLED START
		XME_UNUSED_PARAMETER(status);

		xme_hal_table_rowHandle_t handle;
		autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t* component;

		{
			XME_HAL_TABLE_ITERATE_BEGIN
			(
				componentConfig->components,
				xme_hal_table_rowHandle_t, handle,
				autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t, component
			);
			{
				if (!strcmp(portAddComponentData.name, component->component.name)) //TODO
				{
					if (COMPONENT_STATUS_REMOVED == component->status)
					{
						XME_HAL_TABLE_REMOVE_ITEM(componentConfig->components, handle);
						break;
					}
					else
					{
						printf("Component '%s' already exists!\n", portAddComponentData.name);
						return;
					}
				}
			}
			XME_HAL_TABLE_ITERATE_END();
		}

		if (strcmp(portAddComponentData.parent, "")) //TODO
		{
			bool found = false;

			XME_HAL_TABLE_ITERATE_BEGIN
			(
				componentConfig->components,
				xme_hal_table_rowHandle_t, handle,
				autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t, component
			);
			{
				if (!strcmp(portAddComponentData.parent, component->component.name)) //TODO
				{
					found = true;
					break;
				}
			}
			XME_HAL_TABLE_ITERATE_END();

			if (!found)
			{
				printf("Parent '%s' doesn't exists!\n", portAddComponentData.parent);
			}
		}

		handle = XME_HAL_TABLE_ADD_ITEM(componentConfig->components);
		XME_CHECK(XME_HAL_TABLE_INVALID_ROW_HANDLE != handle, XME_CHECK_RVAL_VOID);

		component = XME_HAL_TABLE_ITEM_FROM_HANDLE(componentConfig->components, handle);
		component->component = portAddComponentData;
		component->status = COMPONENT_STATUS_ADDED;
		component->timeHandle = xme_hal_time_getCurrentTime();

		XME_HAL_TABLE_BUBBLESORT(componentConfig->components, autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t, autoPnP_adv_capabilitiesView_capabilitiesViewComponent_compareComponents);
        // PROTECTED REGION END
    }
    
    {
        // PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_STEP_2_C) ENABLED START
        // PROTECTED REGION END
    }
}

void
autoPnP_adv_capabilitiesView_addComponentFunction_fini
(
    autoPnP_adv_capabilitiesView_capabilitiesViewComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_TERMINATE_C) ENABLED START
	XME_UNUSED_PARAMETER(componentConfig);
    // PROTECTED REGION END
}

// PROTECTED REGION ID(AUTOPNP_ADV_CAPABILITIESVIEW_ADDCOMPONENTFUNCTION_IMPLEMENTATION_C) ENABLED START
int autoPnP_adv_capabilitiesView_capabilitiesViewComponent_compareComponents(autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t* component1, autoPnP_adv_capabilitiesView_capabilitiesViewComponent_component_t* component2)
{
	int result;

	result = strcmp(component1->component.parent, component2->component.parent);
	if (0 == result)
	{
		result = strcmp(component1->component.name, component2->component.name);
	}

	return result;
}
// PROTECTED REGION END
